﻿// WebZHX.API/Controllers/MaterInfoController.cs
using Common.Models;
using Core.Entities.Mater;
using Infrastructure.Services;
using Infrastructure.Services.Mater;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace WebZHX.Controllers.Mater
{
    /// <summary>
    /// 物料信息控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class MaterInfoController : BaseController<Mater_Info, string>
    {
        private readonly MaterInfoService _materInfoService;

        public MaterInfoController(
            BaseService<Mater_Info, string> service,
            MaterInfoService materInfoService)
            : base(service)
        {
            _materInfoService = materInfoService;
        }

        /// <summary>
        /// 根据物料编码查询
        /// </summary>
        /// <param name="materCode">物料编码</param>
        /// <returns>物料信息列表</returns>
        [HttpGet("code/{materCode}")]
        public async Task<ActionResult<IEnumerable<Mater_Info>>> GetByMaterCode(string materCode)
        {
            var result = await _materInfoService.GetByMaterCodeAsync(materCode);
            return Ok(result);
        }

        /// <summary>
        /// 根据物料名称模糊查询
        /// </summary>
        /// <param name="materName">物料名称</param>
        /// <returns>物料信息列表</returns>
        [HttpGet("name/{materName}")]
        public async Task<ActionResult<IEnumerable<Mater_Info>>> GetByMaterName(string materName)
        {
            var result = await _materInfoService.GetByMaterNameAsync(materName);
            return Ok(result);
        }

        public class MaterInfoQueryDto
        {
            /// <summary>
            /// 分页数据
            /// </summary>
            public QueryFilter filter { get; set; }
            /// <summary>
            /// 实体数据
            /// </summary>
            public Mater_Info mater_Info { get; set; }
        }
        /// <summary>
        /// 高级分页查询
        /// </summary>
        /// <param name="filter">查询过滤器</param>
        /// <returns>分页结果</returns>
        [HttpPost("MaterPage")]
        public async Task<ActionResult<PagedResult<Mater_Info>>> AdvancedPagedQuery([FromBody] MaterInfoQueryDto materInfoQueryDto)
        {
            var result = await _materInfoService.GetByMater_InfoAsync(materInfoQueryDto.filter, materInfoQueryDto.mater_Info);
            return Ok(result);
        }
        #region 新增、修改、删除等业务逻辑
        /// <summary>
        /// 保存物料信息
        /// </summary>
        /// <param name="model">物料信息</param>
        /// <returns>创建的物料信息</returns>
        [HttpPost("MaterSave")]
        public async Task<ActionResult<Mater_Info>> SaveMaterInfo(Mater_Info model)
        {
            // 验证必填字段
            if (string.IsNullOrEmpty(model.MaterCode))
            {
                return BadRequest("物料编码不能为空");
            }

            if (string.IsNullOrEmpty(model.MaterName))
            {
                return BadRequest("物料名称不能为空");
            }

            if(string.IsNullOrEmpty(model.Id)){
                model.Id = Guid.NewGuid().ToString();
                model.CreateTime = DateTime.Now.ToString();
                var result = await _materInfoService.CreateAsync(model);//新增
                return CreatedAtAction(nameof(Get), new { id = result.Id }, result);
            }
            else
            {
                model.UpdateTime = DateTime.Now.ToString();
                var result = await _materInfoService.PartialUpdateAsync(model);//修改
                return CreatedAtAction(nameof(Get), new { id = result.Id }, result);
            }
        }
        [HttpDelete("DeleteMater/{ID}")]
        public async Task<ActionResult<Mater_Info>> DeleteFrom(string ID)
        {
            if (string.IsNullOrEmpty(ID))
            {
                throw new Exception("id不能为空"); 
            }
            //软删除
            await _materInfoService.SoftDeleteAsync(ID); 
            return Ok("删除成功");
        }
        #endregion
    }
}